package controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import pojo.Customer;
import service.CustomerManager;
import service.impl.CustomerManagerFactory;

@Controller
@RestController	//是controller的子类，其所有的response都是responsebody
public class CustomerController {
	private CustomerManager manager;
	
	
	public CustomerController() {
		manager = CustomerManagerFactory.getInstance();
	}

//	@RequestMapping(value={"addCustomer"}, method={RequestMethod.POST})
//	public String addCustomer(Customer c, ModelMap model){
//		List<Customer> customers = manager.addCustomers(c);
//		model.addAttribute("customers", customers);
//		return "customer_show";
//	}
	
	@RequestMapping(value="/addCustomer", method=RequestMethod.POST)
	public String addCustomer(Customer c){
		manager.addCustomer(c);
		return "redirect:listAll";
	}
	
	@RequestMapping(value="/listAll")
	public String findAllCustomer(Model model){
		List<Customer> customers = manager.findAll();
		model.addAttribute("customers", customers);
		return "customer_show";
	}
	
	@ResponseBody //直接返回字符串
	@RequestMapping(value="/testJson")
	public String addCustomer1(){
		Customer c= new Customer();
		c.setId("1");
		c.setName("name");
		c.setSal(123);
		
		return "listAll";
	}
	
	
	@ResponseBody //直接返回customer对象的json字符串
	@RequestMapping(value="/testJson2")
	public Customer addCustomer2(){
		Customer c= new Customer();
		c.setId("1");
		c.setName("name");
		c.setSal(123.4);
		c.setSex(true);
		
		return c;
	}
	
	
	

}
